package newfeature.jdk7;

import java.io.IOException;
import java.io.StringReader;

/**
 * Created by dl
 */
public class TryWithResource {
	static class MyStringReader extends StringReader {

		public MyStringReader(String s) {
			super(s);
		}

		@Override
		public void close() {
			System.out.println(".close()");
			super.close();
		}

//		@Override
//		public int read(char[] cbuf, int off, int len) throws IOException {
//			System.out.println(".read(char[], int, int)");
//			return super.read(cbuf, off, len);
//		}
//
//		@Override
//		public int read() throws IOException {
//			System.out.println(".read()");
//			return super.read();
//		}
	}
	public static void main(String[] args) {
		try(MyStringReader reader=new MyStringReader("are you ok")){
			char[] buff=new char[128];
			int n=reader.read(buff);
			System.out.println(new String(buff,0,n));
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println();
		MyStringReader reader2=new MyStringReader("are you ok");
		try(MyStringReader reader3=reader2) {
		}
		System.out.println();
		MyStringReader reader4=new MyStringReader("are you ok");
		try{
			char[] buff=new char[128];
			int n=reader4.read(buff);
			System.out.println(new String(buff,0,n));
		}catch (Exception e){
			e.printStackTrace();
		}
		// reader4 not be closed

		System.out.println("main done");
	}
}
